package com.videoanswer.van.web.client.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.videoanswer.van.service.user.UserService;
import com.videoanswer.van.service.user.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.videoanswer.van.service.Teacher;

@Controller
public class RegisterController {

    @Autowired
    private UserService userService;
	
	/*
	 * student register
	 */
	@RequestMapping(value = { "/studentRegister" }, method = { RequestMethod.GET })
	public ModelAndView studentRegister(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("isUnitPage", true);
		map.put("hasMainNav", false);
		return new ModelAndView("/studentRegister", map);
	}
	
	@RequestMapping(value = { "/studentRegister" }, method = { RequestMethod.POST })
	public ModelAndView postStudentRegister(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();

        UserModel userModel = new UserModel();
		boolean registerSucceed = true;

        userModel.setEmail(request.getParameter("email"));
        userModel.setPassword(request.getParameter("password"));
        userModel.setGrade(request.getParameter("grade"));
        userModel.setProvince(request.getParameter("province"));
        userModel.setCity(request.getParameter("city"));
        userModel.setName(request.getParameter("userName"));
        userModel.setDistrict(request.getParameter("city"));
		
		try{
			userModel = userService.register(userModel);
		}catch(Exception e){
			System.out.println(e.toString());
			registerSucceed = false;
		}
		
		if(null != userModel && registerSucceed){
			map.put("isStudent", true);
			map.put("isTeacher", false);
			map.put("username", userModel.getName());
			return new ModelAndView("/vanOverview", map);
		}else{
			
			map.put("isUnitPage", true);
			map.put("hasMainNav", false);
			return new ModelAndView("/studentRegister", map);
		}
	}
	
	/*
	 * teacher register
	 */
	@RequestMapping(value = { "/teacherRegisterStepOne" }, method = { RequestMethod.GET })
	public ModelAndView teacherRegisterStepOne(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isUnitPage", true);
		map.put("hasMainNav", false);
		
		return new ModelAndView("/teacherRegisterStepOne", map);
	}
	
	
	@RequestMapping(value = { "/teacherRegisterStepTwo" }, method = { RequestMethod.POST })
	public ModelAndView teacherRegisterStepTwo(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		Teacher teacher = new Teacher();
		
		teacher.setTeacherEmail(request.getParameter("email"));
		teacher.setTeacherPassword(request.getParameter("password"));
		teacher.setTeacherSubject(request.getParameter("subject"));
		teacher.setTeacherParagraph(request.getParameter("paragraph"));
		teacher.setTeacherSchool(request.getParameter("school"));
		teacher.setTeacherLevel(request.getParameter("level"));
		teacher.setTeacherResume(request.getParameter("resume"));
		teacher.setTeacherUserName(request.getParameter("userName"));
		teacher.setTeacherGender(request.getParameter("gender"));
		teacher.setTeacherPhoneNum(request.getParameter("phoneNum"));
		teacher.setTeacherLocationProvince(request.getParameter("province"));
		teacher.setTeacherLocationDistrict(request.getParameter("district"));
		teacher.setTeacherIdentifyID(request.getParameter("identifyID"));
		
		map.put("isUnitPage", true);
		map.put("hasMainNav", false);
		map.put("teacher", teacher);
		return new ModelAndView("/teacherRegisterStepTwo", map);
	}
	
	@RequestMapping(value = { "/teacherRegisterStepThree" }, method = { RequestMethod.POST})
	public ModelAndView teacherRegisterStepThree(MultipartHttpServletRequest fileRequest, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("isUnitPage", true);
		map.put("hasMainNav", false);
		return new ModelAndView("/teacherRegisterStepThree", map);
	}

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}